using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
 
 
public class ButtonPress : Button
{
    public bool isPointerDown = false;
    private float timePressStarted;
    private float durationThreshold = 0.05f; // 长按的阈值时间
 
    public UnityEvent OnPress = new UnityEvent(); // 长按事件
    public UnityEvent OnPressEnd = new UnityEvent(); // 长按结束事件
 
    private void Update()
    {
        if (isPointerDown)
        {
            if (Time.time - timePressStarted > durationThreshold) // 检查是否超过阈值
            {
                OnPress.Invoke(); 
                isPointerDown = false;
            }
        }
    }
 
    public override void OnPointerDown(PointerEventData eventData)
    {
        base.OnPointerDown(eventData); // 调用基类的方法
        if (eventData.button != PointerEventData.InputButton.Left)
            return;
 
        timePressStarted = Time.time;
        isPointerDown = true;
    }
 
    public override void OnPointerUp(PointerEventData eventData)
    {
        base.OnPointerUp(eventData); // 调用基类的方法
        isPointerDown = false;
        OnPressEnd.Invoke();
    }
}
